package org.fnzn.web.fngl;

import org.apache.log4j.Logger;
import org.fnzn.aop.SystemControllerLog;
import org.fnzn.bean.FnglHouse;
import org.fnzn.core.Page;
import org.fnzn.core.Result;
import org.fnzn.service.FnglHouseService;
import org.fnzn.util.FileUpload;
import org.fnzn.util.ResultGenerator;
import org.fnzn.web.SysLogController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 房源信息管理
 */
@RestController
@RequestMapping(value = "fnglHouse")
@EnableAutoConfiguration
public class FnglHouseController {
    Logger log = Logger.getLogger(SysLogController.class);

    @Value("${upload.path}")
    private String path;

    @Autowired
    private ResourceLoader resourceLoader;

    @Autowired
    private FnglHouseService fnglHouseService;

    @RequestMapping(value = "/list", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @SystemControllerLog(description = "查询房源")
    public Result findFnglHouse(FnglHouse fnglHouse, Page page) {
        return fnglHouseService.findFnglHouse(fnglHouse, page);
    }

    @RequestMapping(value = "/add", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    @SystemControllerLog(description = "新增房源")
    public Result insertFnglHouse(FnglHouse fnglHouse) {
        return fnglHouseService.insertFnglHouse(fnglHouse);
    }

    @RequestMapping(value = "/update", produces = "application/json;charset=UTF-8", method = RequestMethod.PUT)
    @SystemControllerLog(description = "修改房源")
    public Result updateFnglHouse(FnglHouse fnglHouse) {
        return fnglHouseService.updateFnglHouse(fnglHouse);
    }

    @RequestMapping(value = "/{Id}", produces = "application/json;charset=UTF-8", method = RequestMethod.DELETE)
    @SystemControllerLog(description = "删除房源")
    public Result deleteFnglHouse(@PathVariable String Id) {
        return fnglHouseService.deleteFnglHouse(Id);
    }

    @RequestMapping(value = "/deleteAll", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @SystemControllerLog(description = "删除多个房源")
    public Result deleteAllFnglHouse(String Ids) {
        return fnglHouseService.deleteAllFnglHouse(Ids);
    }

    //处理文件上传
    @RequestMapping(value = "/uploadimg", method = RequestMethod.POST)
    public Map<String,String> uploadImg(@RequestParam("file") MultipartFile file,
                            HttpServletRequest request) {

        //图片文件类型
        String contentType = file.getContentType();
        //图片名字
        String fileName = file.getOriginalFilename();
        //文件存放路径
//        String filePath = "C:\\Users\\lvqi\\Desktop\\";
        String filePath = "/home/fn_estate/picture/";
        //调用文件处理类FileUtil，处理文件，将文件写入指定位置
        try {
            FileUpload.uploadFile(file.getBytes(), filePath, fileName);

        } catch (Exception e) {
            // TODO: handle exception
        }
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();
        Map<String,String> map = new HashMap<>();
        map.put("imgUrl",basePath+"/fnglHouse/getimg/"+fileName);

        // 返回图片的存放路径
        return map;
    }

    @RequestMapping(value = "/getimg/**", method = RequestMethod.GET)
    public ResponseEntity getImg(HttpServletRequest request) {

        String url = request.getRequestURI().replace("fnglHouse/getimg","");
        return ResponseEntity.ok()
                .contentType(MediaType.IMAGE_JPEG)
                .body(resourceLoader.getResource("file:" + path + url));
    }

    /**
     *  根据房源id获取到房源图片
     */
    @RequestMapping(value = "/getHouseImg", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @SystemControllerLog(description = "获取房源图片")
    public String[] getHouseImg(String houseId) {
        return fnglHouseService.getHouseImg(houseId);
    }

    /**
     *  根据选择的二手房门店获取到门店下所有房源
     */
    @RequestMapping(value = "/getHouse",produces = "application/json;charset=UTF-8",method = RequestMethod.GET)
    @SystemControllerLog(description = "获取二手房源")
    public List<FnglHouse> getHouse(String projectId){

        return fnglHouseService.getHouse(projectId);
    }

}
